﻿using System;
using System.Collections.ObjectModel;
using QDT.Common;
using QDT.Core.Domain;
using QDT.Core.Members;
using QDT.Payment;

namespace QDT.Services.Members
{
    public partial class UserService
    {
        public UserCarry CreateCarry(UserCarry carry)
        {
            var user = DbProvider.GetByID<User>(carry.UserId);

            if (user == null) throw new Exception("提现用户不存在，或者已经被删除");


            var log = user.FreezeMoney(UserTransLog.TradeType.CarrayMoneyToLock, carry.Money, "", "申请提现");

            DbProvider.Add(log);

            DbProvider.Add(carry);

            DbProvider.SaveChanges();

            return carry;
        }

        public void CarryCompleted(int carryId, string msg, string desc) {
            var carry = DbProvider.GetByID<UserCarry>(carryId);
            if(carry==null) throw new Exception("提现申请不存在，或者已经被删除");

            if(carry.Status!=0) throw new Exception("提现申请已经处理过，不能重复处理");

            var user = DbProvider.GetByID<User>(carry.UserId);

            if(user==null) throw new Exception("提现用户不存在，或者已经被删除");

            var order = new CarryOrder
            {
                OrderSn = carry.ID.ToString(),
                Amount = carry.Money,
                BankAccount = carry.BankCard.Replace(" ", ""),
                AccountName = user.RealName,
                BankCode = "",
                IdCardNo = "",
                PhoneNo = "",
                Province = "",
                City = "",
                BankZone = "",
            };

            var payment = IoC.Resolve<IPayment>();
            var re = payment.CarryRequest(order);

            if (!re) return;

            carry.SetStatus(6);
            carry.Msg = msg;
            carry.Desc = desc;
            carry.UpdateTime = DateTime.Now;
            DbProvider.SaveChanges();
        }

        public void CarryCompleted(int carryId )
        {
            var carry = DbProvider.GetByID<UserCarry>(carryId);
            if (carry == null) throw new Exception("提现申请不存在，或者已经被删除");
            if (carry.Status != 0) return;

            var user = DbProvider.GetByID<User>(carry.UserId);
            if (user == null) throw new Exception("提现用户不存在，或者已经被删除");
            
            var order = new CarryOrder
            {
                OrderSn = carry.ID.ToString(),
                Amount = carry.Money,
                BankAccount = carry.BankCard.Replace(" ", ""),
                AccountName = user.RealName,
                BankCode = "",
                IdCardNo = "",
                PhoneNo = "",
                Province = "",
                City = "",
                BankZone = "",
            };

            var payment = IoC.Resolve<IPayment>();
            var re = payment.CarryRequest(order);

            if (!re) return;

            carry.SetStatus(6);
            carry.UpdateTime = DateTime.Now;
        }

        public void RefusalCarry(int carryId,string msg,string desc) {
            var carry = DbProvider.GetByID<UserCarry>(carryId);
            if (carry == null) throw new Exception("提现申请不存在，或者已经被删除");

            if (carry.Status != 0 && carry.Status != -1) throw new Exception("提现申请已经处理过，不能重复处理");

            var user = DbProvider.GetByID<User>(carry.UserId);

            if (user == null) throw new Exception("提现用户不存在，或者已经被删除");

            carry.SetStatus(2);
            carry.Msg = msg;
            carry.Desc = desc;
            carry.UpdateTime = DateTime.Now;

            var log = user.UnfreezeMoney(UserTransLog.TradeType.CarrayFaild, carry.Money, "管理员", "提现失败,{0},解除冻结资金".FormatWith(msg));

            DbProvider.Add(log);

            DbProvider.SaveChanges();
        }

        public void CompanyCarry(decimal money)
        {

            var order = new CarryOrder
            {
                OrderSn = DateTime.Now.ToString("ffff"),
                Amount = money,
                BankAccount = "020200190000352",
                AccountName = "遵义中禧互联网金融服务有限公司",
                BankCode = "313",
                Province = "贵州",
                City = "遵义",
                BankZone = "贵州银行股份有限公司遵义分行",
                UnionBank = "313703009999",
                AccountProp = 1
            };

            var payment = IoC.Resolve<IPayment>();
            payment.CarryRequest(order);
        }

    }
}
